package com.project.common.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * 公共的返回值结果类 ，方便对后端接口数据进行统一处理。
 *
 * @author xuxinpeng
 * @version 1.0
 * @date 2021/1/19 16:17
 */

@ApiModel(value = "CommonResult对象", description = "公共返回对象")
@Data
public class CommonResult<T> {

    @ApiModelProperty(value = "返回码")
    private String code;
    @ApiModelProperty(value = "描述")
    private String info;
    @ApiModelProperty(value = "数据对象")
    private T data;


    // 构造方法
    public CommonResult() {
    }

    public CommonResult(T data) {
        this.code = successCode;
        this.info = StringUtils.EMPTY;
        this.data = data;
    }

    public CommonResult(T data, String info) {
        this.code = successCode;
        this.info = info;
        if (data == null) {
            this.data = (T) StringUtils.EMPTY;
        } else {
            this.data = data;
        }
    }


    // 静态方法
    public static CommonResult sysError(String info) {
        CommonResult commonResult = new CommonResult();
        commonResult.setSysError(info);
        return commonResult;
    }

    public static CommonResult interfaceError(String info) {
        CommonResult commonResult = new CommonResult();
        commonResult.setInterfaceError(info);
        return commonResult;
    }

    /**
     * 在无返回值 但是结果正确的时候调用该方法
     *
     * @param info
     * @return
     */
    public static CommonResult success(String info) {
        CommonResult commonResult = new CommonResult();
        commonResult.setSuccess(info);
        commonResult.setData(StringUtils.EMPTY);
        return commonResult;
    }


    // 实例方法
    public void setSuccess(String info) {
        this.code = successCode;
        this.info = info;
    }

    public void setInterfaceError(String info) {
        code = interfaceErrorCode;
        this.info = info;
    }

    public void setSysError(String info) {
        code = sysErrorCode;
        this.info = info;
    }

    //结果码
    // 成功返回CODE
    public static final String successCode = "1000";
    // 接口失败返回code
    private static final String interfaceErrorCode = "1001";
    // 系统自身失败返回code
    private static final String sysErrorCode = "1002";

}

